package cn.edu.sctu.sctu_lysd.my_view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import cn.edu.sctu.sctu_lysd.utils.ScreenUtils;

public class SlidingMenu extends HorizontalScrollView
{
	/**
	 * 屏幕宽度
	 */
	private int mScreenWidth;
	/**
	 * dp
	 */
	private int mMenuRightPadding;
	/**
	 * 菜单的宽度
	 */
	private int mMenuWidth;
	private int mHalfMenuWidth;

	private boolean isOpen;

	private boolean once;

	private StateChangedListener stateChangedListener;

	public void setStateChangedListener(StateChangedListener stateChangedListener) {
		this.stateChangedListener = stateChangedListener;
	}

	public SlidingMenu(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);

	}

	public SlidingMenu(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		mScreenWidth = ScreenUtils.getScreenWidth(context);

		/**
		 * 菜单 距离右边  为 屏幕宽度 * 1/4
		 */
		mMenuRightPadding = mScreenWidth / 4;

//		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
//				R.styleable.SlidingMenu, defStyle, 0);
//		int n = a.getIndexCount();
//		for (int i = 0; i < n; i++)
//		{
//			int attr = a.getIndex(i);
//			switch (attr)
//			{
//			case R.styleable.SlidingMenu_rightPadding:
//				// 默认50
//				mMenuRightPadding = a.getDimensionPixelSize(attr,
//						(int) TypedValue.applyDimension(
//								TypedValue.COMPLEX_UNIT_DIP, 50f,
//								getResources().getDisplayMetrics()));// 默认为10DP
//				break;
//			}
//		}
//		a.recycle();
	}

	public SlidingMenu(Context context)
	{
		this(context, null, 0);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		/**
		 * 显示的设置一个宽度
		 */
		if (!once)
		{
			LinearLayout wrapper = (LinearLayout) getChildAt(0);
			ViewGroup menu = (ViewGroup) wrapper.getChildAt(0);
			ViewGroup content = (ViewGroup) wrapper.getChildAt(1);

			mMenuWidth = mScreenWidth - mMenuRightPadding;
			mHalfMenuWidth = mMenuWidth / 2;
			menu.getLayoutParams().width = mMenuWidth;
			content.getLayoutParams().width = mScreenWidth;

		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		super.onLayout(changed, l, t, r, b);
		if (changed)
		{
			// 将菜单隐藏
			this.scrollTo(mMenuWidth, 0);

			stateChangedListener.menuHide();

			once = true;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev)
	{
		int action = ev.getAction();
		switch (action)
		{
		// Up时，进行判断，如果显示区域大于菜单宽度一半则完全显示，否则隐藏
		case MotionEvent.ACTION_UP:
			int scrollX = getScrollX();
			if (scrollX > mHalfMenuWidth)
			{
				this.smoothScrollTo(mMenuWidth, 0);

				stateChangedListener.menuHide();

				isOpen = false;
			} else
			{
				this.smoothScrollTo(0, 0);

				stateChangedListener.menuDisplay();

				isOpen = true;
			}
			return true;
		}
		return super.onTouchEvent(ev);
	}

	/**
	 * 打开菜单
	 */
	public void openMenu()
	{
		if (isOpen)
			return;
		this.smoothScrollTo(0, 0);

		stateChangedListener.menuDisplay();

		isOpen = true;
	}

	/**
	 * 关闭菜单
	 */
	public void closeMenu()
	{
		if (isOpen)
		{
			this.smoothScrollTo(mMenuWidth, 0);

			stateChangedListener.menuHide();

			isOpen = false;
		}
	}

	/**
	 * 切换菜单状态
	 */
	public void toggle()
	{
		if (isOpen)
		{
			closeMenu();
		} else
		{
			openMenu();
		}
	}


	public interface StateChangedListener{

		/**
		 * menu 显示
		 */
		void menuDisplay();


		/**
		 * menu隐藏
		 */
		void menuHide();


	}


}
